#include "WalkDetectorScene.h"
//#include "BReader.h"
//#include "NodeLoaderLibrary.h"

#include "WalkDetectorNode.h"
#include "WalkTrainingNode.h"

#include "Helpers.h"

USING_NS_CC;

Scene* WalkDetectorScene::scene()
{
    // 'scene' is an autorelease object
    Scene *scene = Scene::create();
    
    // 'layer' is an autorelease object
    WalkDetectorScene *layer = WalkDetectorScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool WalkDetectorScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    this->setAccelerometerEnabled(true);
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    m_mainLayer=Layer::create();
    this->addChild(m_mainLayer);
    m_trainingLayer=Layer::create();
    this->addChild(m_trainingLayer);
    m_detectionLayer=Layer::create();
    this->addChild(m_detectionLayer);
    m_mainLayer->setContentSize(visibleSize);
    
    {
        
        MenuItemLabel *pTrainItem = MenuItemLabel::create(LabelTTF::create("Training", "Arial", 24) ,this,menu_selector(WalkDetectorScene::menuTrainingCallback));
        
        float x=0;
        pTrainItem->setPosition(Point( pTrainItem->getContentSize().width/2 ,
                                        pTrainItem->getContentSize().height/2));
        x+=pTrainItem->getPositionX()+pTrainItem->getContentSize().width/2 + 20;
        
        MenuItemLabel *pDetectItem = MenuItemLabel::create(LabelTTF::create("Detect", "Arial", 24) ,this,menu_selector(WalkDetectorScene::menuDetectCallback));
        
        pDetectItem->setPosition(Point(x+ pDetectItem->getContentSize().width/2 ,
                                       + pDetectItem->getContentSize().height/2));
        
        // create menu, it's an autorelease object
        Menu* pMenu = Menu::create(pTrainItem, pDetectItem,NULL);
        pMenu->setPosition(CCPointZero);
        m_mainLayer->addChild(pMenu, 1);
    }

    {
        
        Array items;
        items.init();
        items.addObject(MenuItemLabel::create(LabelTTF::create("Next", "Arial", 24) ,this,menu_selector(WalkDetectorScene::menuTrainNextCallback)));
        items.addObject(MenuItemLabel::create(LabelTTF::create("Start", "Arial", 24) ,this,menu_selector(WalkDetectorScene::menuTrainStartCallback)));
        items.addObject(MenuItemLabel::create(LabelTTF::create("Restart", "Arial", 24) ,this,menu_selector(WalkDetectorScene::menuTrainRestartCallback)));
        items.addObject(MenuItemLabel::create(LabelTTF::create("Back", "Arial", 24) ,this,menu_selector(WalkDetectorScene::menuBackCallback)));
        
        float x=((MenuItemLabel*)items.getObjectAtIndex(0))->getContentSize().width;
        for (int i=0; i<items.count(); ++i) {
            MenuItemLabel* itm=(MenuItemLabel*)items.getObjectAtIndex(i);
            Size sz=itm->getContentSize();
            itm->setPosition(Point(x+ sz.width/2,sz.height/2));
            x+=sz.width+20;
        }
        // create menu, it's an autorelease object
        Menu* pMenu = Menu::createWithArray(&items);
        pMenu->setPosition(CCPointZero);
        m_trainingLayer->addChild(pMenu, 1);
        m_trainingLayer->setVisible(false);
    }
    {
        
        m_detectionLayer->setVisible(false);
    }
    
    m_stateTxt = LabelTTF::create("State:", "Arial", 24);
    
    // position the label on the center of the screen
    m_stateTxt->setPosition(Point(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - m_stateTxt->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(m_stateTxt, 1);
    
    this->scheduleUpdate();
    
    
    m_detectingNode= WalkDetectorNode::create("");
    m_detectingNode->setContentSize(visibleSize);
    m_detectingNode->setPositionY(25);
    m_detectionLayer->addChild(m_detectingNode);
    
    m_trainingNode= WalkTrainingNode::create();
    m_trainingNode->setContentSize(visibleSize);
    m_trainingNode->setPositionY(25);
    m_trainingLayer->addChild(m_trainingNode);

    return true;
}

void WalkDetectorScene::update(float dt)
{
    if(m_trainingLayer->isVisible())
    {
        char str[256];
        WalkTrainingNode::EStatus status=m_trainingNode->GetStatus();
        switch (status) {
            case WalkTrainingNode::EIdle:
                sprintf(str,"Idle- %s",m_trainingNode->GetCurrentTrainingLabel().c_str());
                break;
            case WalkTrainingNode::ESampling:
                sprintf(str,"Sampling- %s / %d",m_trainingNode->GetCurrentTrainingLabel().c_str(),m_trainingNode->GetCurrentSamples());
                break;
            case WalkTrainingNode::ETraining:
                sprintf(str,"Training - %f",m_trainingNode->GetError());
                break;
            case WalkTrainingNode::EFinished:
                sprintf(str,"Finished: %s",WalkNodeBase::GetTrainingLabels()[m_trainingNode->GetDetectedCycle()].c_str());
                break;
                
            default:
                break;
        }
        m_stateTxt->setString(str);
    }
}

void WalkDetectorScene::onAcceleration(cocos2d::Acceleration *a, cocos2d::Event *event)
{
    if(m_trainingLayer->isVisible())
        m_trainingNode->OnAcceleration(a);
    if(m_detectionLayer->isVisible())
        m_detectingNode->OnAcceleration(a);
}

void WalkDetectorScene::menuTrainingCallback(Object* pSender)
{
    m_mainLayer->setVisible(false);
    m_trainingLayer->setVisible(true);
    
}
void WalkDetectorScene::menuDetectCallback(Object* pSender)
{
    m_mainLayer->setVisible(false);
    m_detectionLayer->setVisible(true);
}


void WalkDetectorScene::menuTrainNextCallback(Object* pSender)
{
    if(m_trainingNode->GetStatus()==WalkTrainingNode::ETraining)
        m_trainingNode->EndTraining();
    else
        m_trainingNode->NextTraining();
    
}
void WalkDetectorScene::menuTrainStartCallback(Object* pSender)
{
    m_trainingNode->StartTraining();
}
void WalkDetectorScene::menuTrainRestartCallback(Object* pSender)
{
    m_trainingNode->RestartTraining();
}

void WalkDetectorScene::menuDetectNextCallback(Object* pSender)
{
}
void WalkDetectorScene::menuBackCallback(Object* pSender)
{
    MenuItem* item=(MenuItem*)pSender;
    item->getParent()->getParent()->setVisible(false);
    m_mainLayer->setVisible(true);
    
    m_trainingNode->Stop();
}